package data
{
	import kinds.MethodOptionKind;
	
	import tag.doABC2.CPool;

	public class Method
	{
		/** 参数个数*/
		public var paramCount:int = 0;
		
		/** 返回类型*/
		public var returnType:int = 0;
		
		/** 参数列表*/
		public var params:Array = null;
		
		/** 函数名字*/
		public var name:String = "";
		
		public var kind:int = 0;
		public var flags:int = 0;
		public var optionCount:int = 0;
		public var optionKinds:Array = null;
		public var optionIndex:Array = null;
		public var paramNames:Array = null;
		public var className:String = null;
		
		public var _cpool:CPool = null;
		public function cpool(cp:CPool):void
		{
			this._cpool = cp;
		}
		
		public function toString():String
		{
			var ret:String = name;
			if(this._cpool != null){
				ret = name + "(";
				if(params != null){
					var hasParamNames:Boolean = (paramNames != null) && (paramNames.length > 0);
					for(var i:int = 0;i < params.length; i++){
						var optionValueStr:String = optionValue(i);
						if(hasParamNames){
							ret += _cpool.cp_string[paramNames[i]];
						}
						ret += (_cpool.cp_multiname[params[i]] as Multiname) ;
						ret += (optionValueStr.length > 0) ? (" = " + optionValueStr) : "";
						ret += (i < (params.length - 1) ? "," : "");
					}
					ret += ")" + (_cpool.cp_multiname[returnType] as Multiname);
				}
			}
			return ret;
		}
		
		private function optionValue(index:int):String
		{
			var ret:String = "";
			if(optionCount > 0 && index < optionCount){
				var kind:uint = optionKinds[index];
				switch(kind){
					case MethodOptionKind.CONSTANT_Int:
						ret = "" + _cpool.cp_int[optionIndex[index]];
						break;
					case MethodOptionKind.CONSTANT_Uint:
						ret = "" + _cpool.cp_uint[optionIndex[index]];
						break;
					case MethodOptionKind.CONSTANT_Double:
						ret = "" + _cpool.cp_double[optionIndex[index]];
						break;
					case MethodOptionKind.CONSTANT_Utf8:
						ret = "\"" + _cpool.cp_string[optionIndex[index]] + "\"";
						break;
					case MethodOptionKind.CONSTANT_True:
						ret = 'true';
						break;
					case MethodOptionKind.CONSTANT_False:
						ret = 'false';
						break;
					case MethodOptionKind.CONSTANT_Null:
						ret = 'null';
						break;
					case MethodOptionKind.CONSTANT_Undefined:
						ret = 'undefined';
						break;
					case MethodOptionKind.CONSTANT_Namespace:
					case MethodOptionKind.CONSTANT_PackageNamespace:
					case MethodOptionKind.CONSTANT_PackageInternalNs:
					case MethodOptionKind.CONSTANT_ProtectedNamespace:
					case MethodOptionKind.CONSTANT_ExplicitNamespace:
					case MethodOptionKind.CONSTANT_StaticProtectedNs:
					case MethodOptionKind.CONSTANT_PrivateNs:
						ret = '' + _cpool.cp_namespace[index];
						break;
				}
			}
			return ret;
		}
	}
}